package net.ikenway.springboot.demo.Controller;

import net.ikenway.springboot.demo.Model.Dto.ProductSortDto;
import net.ikenway.springboot.demo.Model.Dto.ResponseBodyDto;
import net.ikenway.springboot.demo.Service.Implement.ProductSortServiceImpl;
import net.ikenway.springboot.demo.Utils.ValidateErrorMsgUtil;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.persistence.EntityExistsException;
import javax.validation.ConstraintViolationException;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author MasterKenway <zk@ikenway.net>
 * @Description
 * @date Created Date 12/18/2020
 */
@RestController
@RequestMapping("/api/productSort")
public class ProductSortController {
    private final ProductSortServiceImpl productSortService;

    public ProductSortController(ProductSortServiceImpl productSortService) {
        this.productSortService = productSortService;
    }

    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    public ResponseBodyDto findAll(@RequestParam(required = false) Boolean enable) {
        List<ProductSortDto> productSortDtos;
        try {
            if (enable == null) productSortDtos = productSortService.findAll();
            else productSortDtos = productSortService.findAllByEnable(enable);
        } catch (Exception e) {
            return new ResponseBodyDto(500, e.getMessage());
        }
        return new ResponseBodyDto(200, "查询成功", productSortDtos);
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public ResponseBodyDto create(@Valid @RequestBody ProductSortDto productSortDto, BindingResult bindingResult) {
        if (bindingResult.hasErrors())
            return new ResponseBodyDto(500, ValidateErrorMsgUtil.getFirstFormatMsg(bindingResult.getAllErrors()));

        try {
            productSortService.create(productSortDto);
        } catch (EntityExistsException existsException) {
            return new ResponseBodyDto(500, "分类名已存在");
        } catch (Exception e) {
            return new ResponseBodyDto(500, e.getMessage());
        }
        return new ResponseBodyDto(200, "创建成功");
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResponseBodyDto update(@RequestBody ProductSortDto productSortDto) {
        try {
            productSortService.update(productSortDto);
        } catch (Exception e) {
            return new ResponseBodyDto(500, e.getMessage());
        }

        return new ResponseBodyDto(200, "更新成功");
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ResponseBodyDto delete(@RequestBody Map<String, List<Long>> requestData) {
        try {
            List<Long> sortIds = requestData.get("sortIds");
            if (sortIds == null || sortIds.isEmpty()) throw new Exception("数组不能为空");
            productSortService.delete(sortIds);
        } catch (Exception e) {
            return new ResponseBodyDto(500, e.getMessage());
        }

        return new ResponseBodyDto(200, "删除成功");
    }
}
